using System.Collections.Generic;
using RockHopper.Common.Data;
using RockHopper.Common.Events;
using RockHopper.Common.Model;
using RockHopper.Common.Services;
using RockHopper.TrainingCalculator.WinRT.Utils;
using Windows.ApplicationModel;
using Windows.UI.Xaml.Controls;

namespace RockHopper.TrainingCalculator.WinRT.Controls
{
    public class ThresholdControl : Control
    {
        private static readonly ISettingsRepository settingsRepository;
        private static readonly ICalculationService calculationService;
        private Grid grid;
        private readonly List<TextBlock> cells = new List<TextBlock>(); 

        public ThresholdControl()
        {
            DefaultStyleKey = typeof (ThresholdControl);
            DomainEvents.PaceUnitsChanged += OnPaceUnitsChanged;
            DomainEvents.VdotChanged += OnVdotChanged;
        }

        static ThresholdControl()
        {
            if (!DesignMode.DesignModeEnabled)
            {
                settingsRepository = Global.Container.Resolve<ISettingsRepository>();
                calculationService = Global.Container.Resolve<ICalculationService>();
            }
        }

        protected override void OnApplyTemplate()
        {
            base.OnApplyTemplate();

            grid = (Grid) GetTemplateChild("ThresholdGridPart");
            CreateTitle();
            CreateRows();
            CreateColumns();
            UpdateCells(settingsRepository.Vdot, settingsRepository.PaceUnits);
        }

        private void CreateRows()
        {
            for (var i = 0; i < 3; i++)
            {
                grid.RowDefinitions.Add(new RowDefinition());
            }
        }

        private void CreateColumns()
        {
            for (var i = 0; i < 9; i++)
            {
                grid.ColumnDefinitions.Add(new ColumnDefinition());

                var minutes = 20 + 5 * i;
                var header = new TextBlock { Text = minutes.ToString() };
                Grid.SetColumn(header, i);
                Grid.SetRow(header, 1);
                grid.Children.Add(header);

                var cell = new TextBlock();
                Grid.SetColumn(cell, i);
                Grid.SetRow(cell, 2);
                grid.Children.Add(cell);
                cells.Add(cell);
            }
        }

        private void CreateTitle()
        {
            var title = new TextBlock { Text = "Threshold Pace Variation (5min Intervals)" };
            Grid.SetColumnSpan(title, 9);
            grid.Children.Add(title);
        }

        private void OnPaceUnitsChanged(object sender, PaceUnitsChangedEventArgs args)
        {
            UpdateCells(settingsRepository.Vdot, args.Units);
        }

        private void OnVdotChanged(object sender, VdotChangedEventArgs args)
        {
            UpdateCells(args.Vdot, settingsRepository.PaceUnits);
        }

        private void UpdateCells(double vdot, PaceUnitType units)
        {
            if (grid == null) return;

            var context = calculationService.Calculate(vdot);
            var marathonPace = context.Marathon;
            var thresholdPace = context.Threshold;
            var interval = (marathonPace - thresholdPace)/8;

            for (var i = 0; i < cells.Count; i++)
            {
                var x = thresholdPace + i*interval;
                cells[i].Text = x.FormatPace(units);
            }
        }
    }
}